package iot.ht.controller.htaction;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import iot.core.annotation.Auth;
import iot.core.annotation.Log;
import iot.core.data.BasePageDTQto;
import iot.core.data.PageData;
import iot.core.data.entity.HTAction;
import iot.core.data.entity.HTCtl;
import iot.core.data.mapper.HTActionMapper;
import iot.core.data.mapper.HTCtlMapper;
import iot.core.enums.AuthType;
import iot.core.manager.htcll.HTCtlManager;
import iot.core.manager.htcls.HTClsManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/htaction")
@Tag(name = "API管理", description = "")
public class HTActionController {
    @Autowired
    HTActionMapper htActionMapper;

    @Auth(authType = AuthType.Login)
    @Log(logNeed = false,logTemplete ="",logPin = true,logPout = true)
    @PostMapping("/page")
    @Operation(summary = "API分页", description = "")
    public PageData<HTAction> page(@RequestBody BasePageDTQto qm) throws Exception {
        var qw=htActionMapper.getQueryWrapper();
        if(StringUtils.hasText(qm.getDTB() )){
            qw.ge("Edt_DT",qm.getDB() );
        }

        if(StringUtils.hasText(qm.getDTE() )){
            qw.lt("Edt_DT",qm.getDE() );
        }

        if(StringUtils.hasText(qm.getKey() )){
            qw.and(qr->{
                qr.like("CtlFullName",qm.getKey())
                        .or( )
                        .like("Name",qm.getKey())
                        .or()
                        .like("FullUrl",qm.getKey())
                        ;
            });

        }
        var sort=qm.getSort();
        qw.orderBy(true,sort.isIsAsc(),sort.getFiled() );

        var r=htActionMapper.page( qw,qm );
        return r;
    }

    @Auth(authType = AuthType.Auth)
    @Log(logNeed = true,logTemplete ="",logPin = true,logPout = true)
    @PostMapping("/refreshtodb")
    @Operation(summary = "刷新Action", description = "")
    public  int RefreshToDB(){
        HTClsManager.getInstance().refreshToDB();
        HTCtlManager.getInstance().refreshToDB();
        return 1;
    }

}
